import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from '../user/entities/user.entity';
import { Log } from './entities/log.entity';
import { Injectable } from '@nestjs/common';
import { CreateLogDto } from './dto/create-log.dto';
import { IPaginationOptions, paginate, Pagination } from '../utils/paginate';

@Injectable()
export class LogRepository {
  constructor(
    @InjectRepository(Log)
    public repository: Repository<Log>,
  ) {}

  async createLog(createLogDto: CreateLogDto, user: User): Promise<string> {
    if (user) {
      createLogDto['user'] = user;
      createLogDto['userAccount'] = user.account;
    }
    const log = this.repository.create(createLogDto);
    await this.repository.save(log);
    return '添加成功！';
  }
  // async serverLog(data: object) {
  //   // console.log4js('server', data);
  //   const log4js = this.repository.create(data);
  //   console.log4js(log4js);
  //   //   await this.repository.save(log4js);
  //   //   return log4js;
  // }

  async serverLog(options: IPaginationOptions): Promise<Pagination<Log>> {
    const logData = this.repository.createQueryBuilder();
    return await paginate<Log>(logData, options);
  }
}
